Ví dụ Biến thành viên

C++

#include <iostream>class Foo {    int bar; // Member variable  public:    void setBar (const int newBar) { bar = newBar; }};int main () {  Foo rect; // Local variable  return 0;}

Java

class Program{    static void main(final String arguments[])    {    	// This is a local variable. Its lifespan    	// is determined by lexical scope.    	Foo foo;    }}class Foo{    // This is a member variable - a new instance    // of this variable will be created for each     // new instance of Foo.  The lifespan of this    // variable is equal to the lifespan of "this"    // instance of Foo    int bar;}